<?php

namespace Parser\Separator;

use Parser\Enquiry;
use Parser\Parser;
use Parser\Repetition;

class PICCYX implements BaseSeparator
{
    public static $ApiKindList = [
        3000 => '051050', //车损
        4000 => '051051', //三者
        5002 => '051052', //司机
        5001 => '051053', //乘客
    ];

    public function interpreter(array $array, $parser)
    {
        if ($this->error($array, $parser)) {
            return;
        }

        $parser->result = $this->analysis($array);
    }

    /**
     *错误解析
     * Author:ls
     *
     * @param $array
     * @param $parser
     * @return bool
     */
    public function error(&$array, $parser): bool
    {
        if (isset($array['data'])) {
            $array = $array['data'];
        }

        if (($errorTitle = $array['errorTitle'] ?? false) and ($errorMsg = $array['errorMsg'] ?? false)) {
            if (preg_match('/报价次数过于频繁/', $errorMsg)) {
                $errorMsg = '营销系统提示报价次数过于频繁,请稍后重试';
            }
            $parser->message = $errorMsg ?? $errorTitle;
            $parser->code = '500';
            return true;
        }

        if (($errorTitle = $array['data']['errorTitle'] ?? false) and ($errorMsg = $array['data']['errorMsg'] ?? false)) {
            if (preg_match('/报价次数过于频繁/', $errorMsg)) {
                $errorMsg = '营销系统提示报价次数过于频繁,请稍后重试';
            }
            $parser->message = $errorMsg ?? $errorTitle;
            $parser->code = '500';
            return true;
        }

        if ($errorMessage = $array['errorMessage'] ?? false and $errorMessage) {

            if (!preg_match('/此单符合续保条件，已按照续保流程处理，/', $errorMessage)) {
                if (preg_match('/报价次数过于频繁/', $errorMessage)) {
                    $parser->message = '营销系统提示报价次数过于频繁,请稍后重试';
                } else {
                    $parser->message = $errorMessage;
                }
                $parser->code = '500';
                return true;
            }
        }
        return false;
    }


    public function analysis($array): array
    {
        $enquiry = new Enquiry();

        $enquiry->setTaxTotal($array['thisPayTax'] ?? '0');
        $enquiry->setTaxPremium($array['sumPayTax'] ?? '0');

        $enquiry->setTCITotal($array['ciPremium'] ?? '0');
        $enquiry->setTaxPremium($array['ciBenchPremium'] ?? '0');

        $enquiry->setVCITotal($array['biPremium'] ?? '0');
        $enquiry->setVCIPremium($array['biBenchPremium'] ?? '0');
        $enquiry->setVCIDiscount($array['biDiscount'] ?? '0');

        if (isset($array['itemKindTempList'])) {
            foreach ($array['itemKindTempList'] as $kindList) {
                $code = (string)array_search($kindList['kindCode'], self::$ApiKindList);
                if (!$code) {
                    continue;
                }
                $coverages['code'] = (string)array_search($kindList['kindCode'], self::$ApiKindList);
                $coverages['shortName'] = $kindList['kindName'];
                $coverages['insuredDesc'] = (string)$kindList['amount'] ?? '';
                $coverages['total'] = (string)$kindList['premium'];
                $enquiry->setVCICoverages($coverages);
            }
        }
        //商业险费率因子
        if (isset($array['quoteFactorTempList'])) {
            foreach ($array['quoteFactorTempList'] as $value) {
                //自主定价系数
                if ($name = $value['name'] ?? false and preg_match('/自主定价/', $name)) {
                    $enquiry->setIndependentPriceRatio($value['rate']);
                }
                //无赔款优待优惠系数NCD
                if ($name = $value['name'] ?? false and preg_match('/无赔款优待/', $name)) {
                    $enquiry->setNCDRate($value['rate'] ?? '0');
                }
            }
        }

        $enquiry->setTotal();
        if ($repeat = $array['prpReInsureItems'] ?? []) {
            $enquiry->failure();
            if (count($repeat) >= 1) {
                $enquiry->setErrorTip('商业险重复投保');
            }
        }
        $enquiry->success();

        return $enquiry->get();
    }

    //数据解析
    public function repetition($array): Repetition
    {
        $repetition = new Repetition();

        if (isset($array['prpReInsureItems'][0]['itemList']) and $array['prpReInsureItems'][0]['itemList']) {
            $repetitionData = $array['prpReInsureItems'][0];
        } else {
            $repetitionData = $array['prpReInsureItems'][1] ?? false;
        }

        if ($repetitionData) {
            $repetition->setLastSignTime($repetitionData['billDate'] ?? '');
            $repetition->setLastCompany($repetitionData['insurerCode'] ?? '');
            $repetition->setLastStartAt($repetitionData['effectiveDate'] ?? '');
            $repetition->setLastPolicy($repetitionData['policyNo'] ?? '');
            $repetition->setLastEndTime($repetitionData['expireDate'] ?? '');
            $repetition->setLicense($repetitionData['licensePlateNo'] ?? '');

            $coverage = [];

            if ($itemList = $repetitionData['itemList'] ?? []) {
                foreach ($itemList as $list) {
                    if (preg_match('/不计免赔险/', $list['coverageCode'])) {
                        $coverage[] = $list['coverageCode'];
                    } else {
                        array_unshift($coverage, $list['coverageCode']);
                    }
                }
            }
            $repetition->setLastCoverage($coverage);

            if ($repetition->getLastCompany()) {
                $repetition->setStatus('success');
            }
        }
        return $repetition;
    }
}

